﻿#include <iostream>
using namespace std;

#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>

//删除字符使频率相同 - 暴力破解
//https://leetcode.cn/problems/remove-letter-to-equalize-frequency/
class Solution {
public:
    bool equalFrequency(string word) {
        map<char, int> str;
        map<int, int> num;
        for (const auto& c : word)
        {
            ++str[c]; //记录每个字母的数量
        }
        if (str.size() == 1) return true;

        for (const auto& s : str)
        {
            ++num[s.second];
        }

        if (num.size() == 1)
        {
            auto it = num.begin();
            if (it->first == 1 || it->second == 1)
                return true;
        }
        else if (num.size() == 2)
        {
            auto it1 = num.begin();
            auto it2 = ++num.begin();
            //最长单词与最短单词间相差1个 且只有两种字母
            if (abs(it1->first - it2->first) == 1) //如果相差单词数量为1 则最长的那个单词序列一定要是1个
            {
                auto it = it1->first > it2->first ? it1 : it2;
                if (it->second == 1)
                {
                    return true;
                }
            }
            else if (it1->second == 1 && it2->second == 1 && (it1->first == 1 || it2->first == 1))
            {
                return true;
            }

            if (it1->first == 1 && it1->second == 1 || it2->first == 1 && it2->second == 1)
            {
                return true;
            }
        }
        return false;
    }
};
